package com.mofang.powerdekorhelper.activity.mine;

import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.adapter.MinePointAdater;
import com.mofang.powerdekorhelper.base.MvpActivity;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewList;
import com.mofang.powerdekorhelper.model.MinePoint2;
import com.mofang.powerdekorhelper.persenter.MinePointDetailPersenter;
import com.mofang.powerdekorhelper.utils.SharePerforenceUtils;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.view.MinePointDetailView;
import com.mofang.powerdekorhelper.witget.MySwipeRefreshLayout;
import com.mofang.powerdekorhelper.witget.TitleBar;

import butterknife.BindView;


/**
 * 我的积分
 * Created by luofei on 2016/9/26 0026.
 */
public class MinePointDetailActivity extends MvpActivity<MinePointDetailView, MinePointDetailPersenter> implements
        SwipeRefreshLayout.OnRefreshListener,
        BaseRecycleViewList.MyRecycleOnScrollListener,
        MinePointDetailView {

    @BindView(R.id.mine_point_detail_titlebar)
    TitleBar mTitleBar;

    @BindView(R.id.mine_point_detail_recycle)
    BaseRecycleViewList mRecyclerView;

    private int currentPage = 1;

    private int pageSize = 10;

    @BindView(R.id.mine_point_detail_swipe_refresh)
    MySwipeRefreshLayout swipeRefresh;

    int customerId;
    int pointType;

    MinePointAdater adater;

    boolean isLoadAll;

    @Override
    public MinePointDetailPersenter initPresenter() {
        return new MinePointDetailPersenter();
    }

    @Override
    public void initView() {
        inflateLayout(R.layout.activity_minepoint_detail_layout);
        initTitleBar();
        mRecyclerView.setListOnScrollListener(this);
        swipeRefresh.setOnRefreshListener(this);
    }

    @Override
    public void initData() {
        pointType = getIntent().getIntExtra("IntegralType", -1);
        customerId = SharePerforenceUtils.getInstance(this).getUserID();
        presenter.getMinePoint(currentPage, pageSize, customerId, pointType);
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        isLoadAll = false;
        presenter.getMinePoint(currentPage, pageSize, customerId, pointType);
    }

    @Override
    public void setMinePoint(MinePoint2 minePoint) {

        if (currentPage == 1) {
            adater = new MinePointAdater(minePoint.getResult().getData(), this, R.layout.mine_point_list_item_layout);
            mRecyclerView.setAdapter(adater);
        } else {
            if (minePoint.getResult().getData().size() > 0) {
                adater.addData(minePoint.getResult().getData());
            } else {
                isLoadAll = true;
                //toast("没有更多积分了");
            }

        }

        hideProgress();
    }

    // 加载更多
    @Override
    public void loadMore() {

        if (!isLoadAll) {
            presenter.getMinePoint(++currentPage, pageSize, customerId, pointType);
        }
    }

    private void initTitleBar() {
        initTitleBarWithback(mTitleBar, R.string.mine_point_mall);
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {
        swipeRefresh.hide();
    }

    @Override
    public void toast(CharSequence s) {
        T.showShort(this, s);
    }

    @Override
    public void toast(int id) {
        T.showShort(this, getResources().getString(id));
    }

    @Override
    public void showNullLayout() {

    }

    @Override
    public void hideNullLayout() {

    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {
        hideProgress();
    }

    @Override
    public void hideErrorLayout() {

    }

    @Override
    public void onError(String errorInfo) {

    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
